import {Injectable, NestMiddleware} from '@nestjs/common';

@Injectable()
export class AdminauthMiddleware implements NestMiddleware {
    use(req: any, res: any, next: () => void) {
        const excludes: string[] = ['/admin/auth/login', '/admin/auth/doLogin', '/admin/auth/code'];

        const userinfo = req.session.userinfo;

        if (userinfo && userinfo.username) {
            res.locals.userinfo = userinfo;
            next();
        } else {
            if (excludes.includes(req.baseUrl)) {
                next();
            } else {
                res.redirect(301, '/admin/auth/login')
            }
        }
    }
}
